package com.example.goodsserver.controller;

import com.example.goodsserver.params.GoodsParams;
import com.example.goodsserver.params.PageGoodsParams;
import com.example.goodsserver.service.GoodsService;
import com.example.goodsserver.vo.GoodsInfoVO;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.vo.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/goods-server")
@Api(tags = "商品接口")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "获取分页商品")
    @PostMapping(value = "/page")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result<PageInfo<GoodsInfoVO>> pageGoods(@RequestBody PageGoodsParams pageGoodsParams) {
        return new Result(goodsService.pageGoods(pageGoodsParams), ReturnEnum.SUCCESS);
    }

    @ApiOperation(value = "获取分页商品个人商品")
    @PostMapping(value = "/pageGoodsPersonal")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result<PageInfo<GoodsInfoVO>> pageGoodsPersonal(@RequestBody PageGoodsParams pageGoodsParams) {
        return new Result(goodsService.pageGoodsPersonal(pageGoodsParams), ReturnEnum.SUCCESS);
    }

    @ApiOperation(value = "获取商品")
    @GetMapping(value = "/get")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result<GoodsInfoVO> getGoods(@RequestParam String id) {
        return new Result(goodsService.getGoods(id), ReturnEnum.SUCCESS);
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/delete")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result deleteGoods(@RequestParam String id) {
        goodsService.deleteGoods(id);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    @ApiOperation(value = "修改商品")
    @PutMapping(value = "/update")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result updateGoods(@RequestBody GoodsParams goodsParams) {
        goodsService.updateGoods(goodsParams);
        return new Result(null, ReturnEnum.SUCCESS);
    }

    @ApiOperation(value = "新增商品")
    @PostMapping(value = "/insert")
    @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true,
            paramType = "header")})
    public Result insertGoods(@RequestBody GoodsParams goodsParams) {
        return goodsService.insertGoods(goodsParams);
    }

}
